package com.xiaohang.springstudy.chapter1.config;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.context.annotation.Configuration;

/**
 * DO/Schema属性与数据库字段名对应关系配置
 */
//@Configuration
public class MyPhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {
    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
        String propertyName = name.getText();
        if (propertyName.endsWith("Do")) {
            propertyName = propertyName.substring(0, propertyName.length() - 2).toUpperCase();
        } else if (propertyName.endsWith("Schema")) {
            propertyName = propertyName.substring(0, propertyName.length() - 6).toUpperCase();
        }
        return new Identifier(propertyName, name.isQuoted());
    }

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        String propertyName = name.getText().toUpperCase();
        return new Identifier(propertyName, name.isQuoted());
    }
}
